// admin_deposit.js
document.addEventListener('DOMContentLoaded', function() {
    // 添加存款类型按钮
    const addDepositBtn = document.getElementById('addDepositBtn');
    const depositModal = new bootstrap.Modal(document.getElementById('depositModal'));

    if (addDepositBtn) {
        addDepositBtn.addEventListener('click', function() {
            document.getElementById('modalTitle').textContent = '添加存款类型';
            document.getElementById('formAction').value = 'add';
            document.getElementById('savingID').value = '';
            document.getElementById('savingName').value = '';
            document.getElementById('descrip').value = '';
            depositModal.show();
        });
    }

    // 编辑存款类型按钮
    const editBtns = document.querySelectorAll('.edit-btn');
    editBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            const savingID = this.getAttribute('data-id');
            const savingName = this.getAttribute('data-name');
            const descrip = this.getAttribute('data-desc') || '';

            document.getElementById('modalTitle').textContent = '编辑存款类型';
            document.getElementById('formAction').value = 'edit';
            document.getElementById('savingID').value = savingID;
            document.getElementById('savingName').value = savingName;
            document.getElementById('descrip').value = descrip;

            depositModal.show();
        });
    });

    // 删除存款类型按钮
    const deleteBtns = document.querySelectorAll('.delete-btn');
    const deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));

    deleteBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            const savingID = this.getAttribute('data-id');
            const savingName = this.getAttribute('data-name');

            document.getElementById('deleteSavingID').value = savingID;
            document.getElementById('deleteDepositName').textContent = savingName;

            deleteModal.show();
        });
    });

    // 搜索按钮
    const searchBtn = document.getElementById('searchBtn');
    if (searchBtn) {
        searchBtn.addEventListener('click', function() {
            const savingNameSearch = document.getElementById('savingNameSearch').value;
            const descripSearch = document.getElementById('descripSearch').value;

            const url = new URL(window.location.href);
            url.searchParams.set('savingNameSearch', savingNameSearch);
            url.searchParams.set('descripSearch', descripSearch);
            url.searchParams.set('page', 1);

            window.location.href = url.toString();
        });
    }

    // 按Enter键搜索
    const searchInputs = document.querySelectorAll('#savingNameSearch, #descripSearch');
    searchInputs.forEach(input => {
        input.addEventListener('keyup', function(event) {
            if (event.key === 'Enter') {
                searchBtn.click();
            }
        });
    });
});